layui.use(['form', 'table'], function () {
	let $ = layui.$,
		form = layui.form,
		table = layui.table
	// 渲染一次select
	form.render('select')

	//数据表格
	// let loading = layer.msg("数据请求中...");
	table.render({
		elem: '#LAY-manage',
		url: 'http://localhost:5000/tdx/function/list',
		loading: true,
		cellMinWidth: 100,
		cols: [
			[
				// {type: "checkbox"},
				// {title: 'ID', field: 'id', width: 100},
				{ title: '用户名', field: 'userName', width: 100 },
				{ title: '名称', field: 'name', width: 150 },
				{
					title: '所属平台',
					field: 'platform',
					width: 100,
					templet(d) {
						if (d.platform === 'iwencai') {
							return '酷财经'
						} else if (d.platform === 'tdx') {
							return '通达信'
						}
					},
				},
				{
					title: '共享',
					field: 'isShare',
					width: 100,
					templet: function (d) {
						if (d.isShare === 0) {
							return '<span style="color: #FF5722">隐藏</span>'
						} else if (d.isShare === 1) {
							return '<span style="color: #1890FF">共享</span>'
						}
					},
				},
				{ title: '策略', field: 'content' },
				{
					field: 'status',
					title: '状态',
					width: 100,
					templet: function (d) {
						if (d.status === 0) {
							return '<span style="color: #FF5722">无效</span>'
						} else if (d.status === 1) {
							return '<span style="color: #1890FF">正常</span>'
						}
					},
				},
				{ title: '创建时间', field: 'gmtCreate', width: 180 },
				{ templet: '#table-list', title: '操作', width: 200 },
			],
		],
		limits: [10, 15, 20, 25],
		limit: 10,
		page: true,
		done: function () {
			// layer.close(loading);
		},
	})
	//监听搜索
	form.on('submit(LAY-front-search)', function (data) {
		var field = data.field
		//执行重载
		table.reload('LAY-manage', {
			where: field,
			page: {
				curr: 1, //重新从第 1 页开始
			},
		})
	})

	table.on('tool(LAY-manage)', function (e) {
		if ('del' === e.event) {
			layer.confirm('确定删除吗？', function (t) {
				let ids = e.data.id
				$.ajax({
					type: 'GET',
					url:
						'http://localhost:5000/tdx/function/delete/' +
						e.data.id,
					dataType: 'json',
					success: function (json) {
						layer.close(t)
						if (0 === json.code) {
							layer.msg('删除成功')
							table.reload('LAY-manage')
						} else {
							layer.msg(json.msg)
						}
					},
					error: function () {
						layer.msg('请求失败')
					},
				})
			})
		} else if ('edit' === e.event) {
			location.hash = '/modules/tdx/functionEdit/funId=' + e.data.id
		} else if ('enable' === e.event) {
			let loading = layer.msg('正在处理,请稍等...', {
				icon: 16,
				shade: 0.3,
				time: 0,
			})
			$.ajax({
				type: 'GET',
				url:
					'http://localhost:5000/tdx/function/enable/' +
					e.data.id,
				dataType: 'json',
				success: function (json) {
					if (0 === json.code) {
						layer.msg('操作成功')
						table.reload('LAY-manage')
					} else {
						layer.msg(json.msg)
					}
				},
				error: function () {
					layer.msg('请求失败')
				},
				complete: function (jqXHR, textStatus) {
					layer.close(loading)
				},
			})
		} else if ('disable' === e.event) {
			let loading = layer.msg('正在处理,请稍等...', {
				icon: 16,
				shade: 0.3,
				time: 0,
			})
			$.ajax({
				type: 'GET',
				url:
					'http://localhost:5000/tdx/function/disable/' +
					e.data.id,
				dataType: 'json',
				success: function (json) {
					if (0 === json.code) {
						layer.msg('操作成功')
						table.reload('LAY-manage')
					} else {
						layer.msg(json.msg)
					}
				},
				error: function () {
					layer.msg('请求失败')
				},
				complete: function (jqXHR, textStatus) {
					layer.close(loading)
				},
			})
		} else if ('favorite' === e.event) {
			let loading = layer.msg('正在处理,请稍等...', {
				icon: 16,
				shade: 0.3,
				time: 0,
			})
			$.ajax({
				type: 'GET',
				url:
					'http://localhost:5000/tdx/function/favorite/' +
					e.data.id,
				dataType: 'json',
				success: function (json) {
					if (0 === json.code) {
						layer.msg('操作成功')
						table.reload('LAY-manage')
					} else {
						layer.msg(json.msg)
					}
				},
				error: function () {
					layer.msg('请求失败')
				},
				complete: function (jqXHR, textStatus) {
					layer.close(loading)
				},
			})
		} else if ('cancelFavorite' === e.event) {
			let loading = layer.msg('正在处理,请稍等...', {
				icon: 16,
				shade: 0.3,
				time: 0,
			})
			$.ajax({
				type: 'GET',
				url:
					'http://localhost:5000/tdx/function/cancelFavorite/' +
					e.data.id,
				dataType: 'json',
				success: function (json) {
					if (0 === json.code) {
						layer.msg('操作成功')
						table.reload('LAY-manage')
					} else {
						layer.msg(json.msg)
					}
				},
				error: function () {
					layer.msg('请求失败')
				},
				complete: function (jqXHR, textStatus) {
					layer.close(loading)
				},
			})
		}
	})
	//事件
	var active = {
		add: function () {},
	}

	$('.layui-btn.layui-btn-normal').on('click', function () {
		let type = $(this).data('type')
		active[type] ? active[type].call(this) : ''
	})
})
